<?php
namespace app\custom_user\validate;

use think\Validate;
use app\custom_user\model\UserSession;

class UserValidate extends Validate
{
    protected $field = [
        "id"=>"用户id",
        "username"=>"账号",
        "password"=>"密码",
        "old_password"=>"旧密码",
        "new_password"=>"新密码",
        "mobile"=>"手机号",
        "email"=>"电子邮箱",
        "code"=>"验证码",
    ];
    protected $rule = [
        "id"=>"require",
        "username"=>"require",
        "password"=>"require",
        "old_password"=>"require",
        "new_password"=>"require",
        "mobile"=>"require",
        "email"=>"require",
        "token"=>"checkToken",
        "code"=>"require",
    ];
    protected static $typeMsg = [
        'require'=>':attribute 不能为空',
    ];
    protected $scene = [
        'register'=>["username","password"=>["require","checkPassword"]],
        'login'=>["username","password"],
        'update_mobile'=>["mobile","code"],
        'update_password'=>["old_password","new_password"=>["require","checkPassword"]],
        'getback_password'=>["username","code","new_password"=>["require","checkPassword"]],
    ];
    //如果包含token，则使用token初始化session
    protected function checkToken($value,$rule,$data)
    {
        UserSession::init($value);
        return true;
    }
    //检查密码是否符合要求(只在注册和修改密码时检查新密码)
    protected function checkPassword($value, $rule, $data, $field, $title)
    {
        if(strlen($value)<6){
            return "密码长度不能小于六位";
        }
        if(preg_match("/^[0-9]+$/",$value)){
            return "不允许使用纯数字密码";
        }
        return true;
    }
}
